package com.easy.util;

import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import org.springframework.transaction.annotation.Transactional;

import java.util.Properties;

public class EmailUtil {

    private static final String FROM = "1297568678@qq.com";
    private static final String AUTH_CODE = "qotvifunlwegiecc";
    private static final String HOST = "smtp.qq.com";
    private static final String PORT = "465";//端口号

  private static final Session session;
  static {
      Properties props = new Properties();
      props.setProperty("mail.smtp.host", HOST);
      props.setProperty("mail.smtp.port", PORT);
      props.setProperty("mail.smtp.auth", "true");
      props.setProperty("mail.smtp.ssl.enable", "true");
      session = Session.getInstance(props, new Authenticator() {
          @Override
          protected PasswordAuthentication getPasswordAuthentication() {
              return new PasswordAuthentication(FROM, AUTH_CODE);
          }
      });
//      // 开启调试模式  会在控制台打印日志
//      session.setDebug(true);
  }




    public static void sendEmail(String to, String text) {
        Message message = new MimeMessage(session);
        try {
            message.addFrom(new Address[]{new InternetAddress(FROM)});
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));//to就是发到哪里去
            message.setSubject("托尼快剪验证码");

            message.setContent("<h1 style='color:red'>验证码:"+text+"</h1>", "text/html;charset=utf-8");
            Transport.send(message);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }

    }
    public static void main(String[] args) {
        sendEmail("2562826015@qq.com","123456");
    }
}
